class Observer {
  constructor(data) {
    this.walk(data)
  }

  walk(data) {
    // 前置判断data类型
    if (!data || typeof data !== 'object') return
    //
    Object.keys(data).forEach(key => {
      this.defineReactive(data, key, data[key])
    })
  }

  defineReactive(obj, key, val) {
    // 如果值为对象，需递归转换对象的键
    this.walk(val)
    const that = this
    const dep = new Dep()
    Object.defineProperty(obj, key, {
      enumerable: true,
      configurable: true,
      get() {
        Dep.target && dep.addSub(Dep.target)
        return val
      },
      set(newval) {
        if (newval === val) return
        val = newval
        that.walk(newval)

        // 发送通知
        dep.notify()
      }
    })
  }
}